/*
 * @author hzq
 * @date 2024-12-01 18:59:21
 * @description 颜色转换工具
 * @see 🚀 https://juejin.cn/post/7399592120146313243
 * @status done
 */

export default {
  /**
   * hex颜色转rgb颜色
   * @param str 16进制颜色
   * @returns
   */
  HexToRgb(str: string) {
    str = str.replace('#', '')
    const hxs: any = str.match(/../g)
    for (let i = 0; i < 3; i++) {
      hxs[i] = parseInt(hxs[i], 16)
    }
    return hxs
  },
  /**
   * rgb颜色转hex颜色
   * @param a r
   * @param b
   * @param c
   * @returns
   */
  RgbToHex(a: number, b: number, c: number) {
    const hexs = [a.toString(16), b.toString(16), c.toString(16)]
    for (let i = 0; i < 3; i++) {
      if (hexs[i].length == 1) hexs[i] = '0' + hexs[i]
    }
    return '#' + hexs.join('')
  },
  /**
   * 加深
   * @param color 16进制颜色
   * @param level
   * @returns
   */
  darken(color: string, level: number) {
    const rgbc = this.HexToRgb(color)
    for (let i = 0; i < 3; i++) rgbc[i] = Math.floor(rgbc[i] * (1 - level))
    return this.RgbToHex(rgbc[0], rgbc[1], rgbc[2])
  },
  /**
   * 变淡
   * @param color 16进制颜色
   * @param level
   * @returns
   */
  lighten(color: string, level: number) {
    const rgbc = this.HexToRgb(color)
    for (let i = 0; i < 3; i++)
      rgbc[i] = Math.floor((255 - rgbc[i]) * level + rgbc[i])
    return this.RgbToHex(rgbc[0], rgbc[1], rgbc[2])
  }
}
